package org.example;

import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class helpView  {
    public helpView() {
        initComponents();
    }

    private void initComponents() {

        helpViewForm = new JFrame("帮助界面");
        helpViewForm.setVisible(true);
        helpViewForm.setSize(380, 520);
        helpViewForm.setResizable(false);
        helpViewForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        helpViewForm.setLocationRelativeTo(null);

        lostPasswordLabel = new JLabel();
        changePasswordLabel = new JLabel();
        nameLabel1 = new JLabel();
        wordLabel = new JLabel();
        findPasswordButton = new JButton();
        nameLabel2 = new JLabel();
        passwordLabel = new JLabel();
        changePasswordButton = new JButton();
        wordLabel2 = new JLabel();
        nameField1 = new JTextField();
        nameField2 = new JTextField();
        wordField1 = new JTextField();
        passwordField = new JPasswordField();
        passwordField1 = new JPasswordField();
        createAccountButton = new JButton();
        backButton = new JButton();
        deleteButton = new JButton();

        {
            var helpViewFormContentPane = helpViewForm.getContentPane();
            helpViewFormContentPane.setLayout(null);

            lostPasswordLabel.setText("忘记密码");
            lostPasswordLabel.setFont(lostPasswordLabel.getFont().deriveFont(lostPasswordLabel.getFont().getSize() + 20f));
            helpViewFormContentPane.add(lostPasswordLabel);
            lostPasswordLabel.setBounds(30, 30, 140, 45);

            changePasswordLabel.setText("修改密码");
            changePasswordLabel.setFont(changePasswordLabel.getFont().deriveFont(changePasswordLabel.getFont().getSize() + 20f));
            helpViewFormContentPane.add(changePasswordLabel);
            changePasswordLabel.setBounds(30, 230, 140, 45);

            nameLabel1.setText("用户名");
            nameLabel1.setFont(nameLabel1.getFont().deriveFont(nameLabel1.getFont().getSize() + 7f));
            helpViewFormContentPane.add(nameLabel1);
            nameLabel1.setBounds(new Rectangle(new Point(50, 90), nameLabel1.getPreferredSize()));

            findPasswordButton.setText("查询密码");
            findPasswordButton.setFont(findPasswordButton.getFont().deriveFont(findPasswordButton.getFont().getSize() + 3f));
            findPasswordButton.addActionListener(e -> {
                try {
                    password(e);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            helpViewFormContentPane.add(findPasswordButton);
            findPasswordButton.setBounds(40, 170, 100, 40);

            backButton.setText("返回");
            backButton.setFont(backButton.getFont().deriveFont(backButton.getFont().getSize() + 3f));
            backButton.addActionListener(e -> {
                new loginView();
                helpViewForm.dispose();
            });
            helpViewFormContentPane.add(backButton);
            backButton.setBounds(200, 170, 100, 40);

            deleteButton.setText("注销账户");
            deleteButton.setFont(deleteButton.getFont().deriveFont(deleteButton.getFont().getSize() + 3f));
            deleteButton.addActionListener(e -> {
                try {
                    delete(e);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            helpViewFormContentPane.add(deleteButton);
            deleteButton.setBounds(200, 230, 100, 40);

            nameLabel2.setText("用户名");
            nameLabel2.setFont(nameLabel2.getFont().deriveFont(nameLabel2.getFont().getSize() + 7f));
            helpViewFormContentPane.add(nameLabel2);
            nameLabel2.setBounds(50, 290, 60, 25);

            passwordLabel.setText("密码");
            passwordLabel.setFont(passwordLabel.getFont().deriveFont(passwordLabel.getFont().getSize() + 7f));
            helpViewFormContentPane.add(passwordLabel);
            passwordLabel.setBounds(70, 330, 57, 25);

            changePasswordButton.setText("修改密码");
            changePasswordButton.setFont(changePasswordButton.getFont().deriveFont(changePasswordButton.getFont().getSize() + 3f));
            changePasswordButton.addActionListener(e -> {
                try {
                    update(e);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            helpViewFormContentPane.add(changePasswordButton);
            changePasswordButton.setBounds(40, 410, 100, 40);

            createAccountButton.setText("注册账户");
            createAccountButton.setFont(createAccountButton.getFont().deriveFont(createAccountButton.getFont().getSize() + 3f));
            createAccountButton.addActionListener(e -> {
                try {
                    add(e);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            helpViewFormContentPane.add(createAccountButton);
            createAccountButton.setBounds(200, 410, 100, 40);

            wordLabel2.setText("确认密码");
            wordLabel2.setFont(wordLabel2.getFont().deriveFont(wordLabel2.getFont().getSize() + 7f));
            helpViewFormContentPane.add(wordLabel2);
            wordLabel2.setBounds(50, 370, 80, 25);

            helpViewFormContentPane.add(nameField1);
            nameField1.setBounds(145, 90, 180, 30);
            helpViewFormContentPane.add(nameField2);
            nameField2.setBounds(145, 290, 180, 30);
            helpViewFormContentPane.add(passwordField);
            passwordField.setBounds(145, 330, 180, 30);
            helpViewFormContentPane.add(passwordField1);
            passwordField1.setBounds(145, 370, 180, 30);
        }
    }

    private void delete(ActionEvent e) throws IOException {
        //加载mybatis核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //获取SqlSession对象,来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //执行sql
        String username = nameField2.getText();
        String password = new String(passwordField.getPassword());
        String word = new String(passwordField1.getPassword());
        boolean flag;
        if(word.equals(password) && !username.isEmpty() && !password.isEmpty() && !word.isEmpty()){
            flag = true;
        }else{
            flag = false;
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        if(flag){
            userMapper.delete(user);

            //刷新
            sqlSession.commit();

            //释放资源
            sqlSession.close();
            JOptionPane.showMessageDialog(null,"注销成功");
            new loginView();
            helpViewForm.dispose();
        }else{
            JOptionPane.showMessageDialog(null,"注销失败");
        }
    }

    private void password(ActionEvent e) throws IOException {
        //加载mybatis核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //获取SqlSession对象,来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //执行sql
        String username = nameField1.getText();
        if(userMapper.password(username) == 1){
            List<User> users = userMapper.selectByUsername(username);
            for (User user : users) {
                JOptionPane.showMessageDialog(null,user);
            }
        }
    }

    private void update(ActionEvent e) throws IOException {
        //加载mybatis核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //获取SqlSession对象,来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //执行sql
        String username = nameField2.getText();
        String password = new String(passwordField.getPassword());
        String word = new String(passwordField1.getPassword());
        boolean flag;
        if(word.equals(password) && !username.isEmpty() && !password.isEmpty() && !word.isEmpty()){
            flag = true;
        }else{
            flag = false;
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        boolean flagAccount;
        if(userMapper.username(username) == 1){
            flagAccount = true;
        }else{
            flagAccount = false;
        }
        if(flag && flagAccount){
            userMapper.update(user);

            //刷新
            sqlSession.commit();

            sqlSession.close();
            JOptionPane.showMessageDialog(null,"修改成功!");
            new loginView();
            helpViewForm.dispose();
        }else{
            JOptionPane.showMessageDialog(null,"修改失败!检查密码确认!");
        }
    }
    private void add(ActionEvent e) throws IOException {
        //加载mybatis核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //获取SqlSession对象,来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //执行sql
        String username = nameField2.getText();
        String password = new String(passwordField.getPassword());
        String word = new String(passwordField1.getPassword());
        boolean flag;
        if(word.equals(password) && !username.isEmpty() && !password.isEmpty() && !word.isEmpty()){
            flag = true;
        }else{
            flag = false;
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        if(flag){
            userMapper.add(user);

            //刷新
            sqlSession.commit();

            //释放资源
            sqlSession.close();
            JOptionPane.showMessageDialog(null,"注册成功");
            new loginView();
            helpViewForm.dispose();
        }else{
            JOptionPane.showMessageDialog(null,"注册失败");
        }
    }

    private  JFrame helpViewForm;
    private JLabel lostPasswordLabel;
    private JLabel changePasswordLabel;
    private JLabel nameLabel1;
    private JLabel wordLabel;
    private JButton findPasswordButton;
    private JLabel nameLabel2;
    private JLabel passwordLabel;
    private JButton changePasswordButton;
    private JLabel wordLabel2;
    private JTextField nameField1;
    private JTextField nameField2;
    private JTextField wordField1;
    private JPasswordField passwordField;
    private JPasswordField passwordField1;
    private JButton createAccountButton;
    private JButton backButton;
    private JButton deleteButton;

    public static void main(String[] args) {
        new helpView();
    }
}
